package com.ctsi.framework.common.exception;

import com.ctsi.framework.common.response.UnifiedResponse;
import org.springframework.context.MessageSource;

import java.util.Locale;
import java.util.Map;

/**
 * @author: zhangjw
 * @Date: 2018/9/16 18:10
 * @Description:
 */
public class ExceptionFactory {

    private MessageSource messageSource;

    public ExceptionFactory(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    private static final Locale DEFALUT_LOCALE = Locale.CHINA;

    public String getResource(String code, Object... args) {
        return messageSource.getMessage(String.valueOf(code), args, ExceptionFactory.DEFALUT_LOCALE);
    }
    public String getResource(String code) {
        return messageSource.getMessage(String.valueOf(code), null, ExceptionFactory.DEFALUT_LOCALE);
    }

    public UnifiedResponse<Void> createInternalError() {
        return UnifiedResponse.error(CoreErrorConstant.ERROR_CODE,getResource(CoreErrorConstant.ERROR_CODE));
    }

    public UnifiedResponse<Map<String,Object>> createInternalError(Map<String,Object> addtion) {
        return UnifiedResponse.error(CoreErrorConstant.ERROR_CODE,getResource(CoreErrorConstant.ERROR_CODE),addtion);
    }

    /**
     *
     */
    public void throwInternalError() {
        throw new BasedException(CoreErrorConstant.ERROR_CODE, getResource(CoreErrorConstant.ERROR_CODE));
    }

    /**
     *
     */
    public void throwInternalError(Map<String,Object> addtion) {
        throw new BasedException(CoreErrorConstant.ERROR_CODE, getResource(CoreErrorConstant.ERROR_CODE),addtion);
    }

    /**
     *
     * @param code
     * @param args
     */
    public void throwDefinedException(String code, Object... args) {
        throw new BasedException(code, getResource(code, args));
    }

    public void throwDefinedException(String code,String msg){
        throw new BasedException(code, msg);
    }

    /**
     *
     * @param code
     * @param args
     */
    public void throwDefinedException(String code,Map<String,Object> addtion, Object... args) {
        throw new BasedException(code, getResource(code, args),addtion);
    }

    /**
     *
     * @param code
     * @param args
     */
    public void throwAuthError(String code, Object... args) {
        throw new AuthErrorException(code, getResource(code, args));
    }

    /**
     *
     * @param code
     * @param args
     */
    public void throwAuthError(String code,Map<String,Object> addtion, Object... args) {
        throw new AuthErrorException(code, getResource(code, args),addtion);
    }

    /**
     *
     * @param code
     * @param args
     */
    public void throwObjectNotFound(String code, Object... args) {
        throw new ResourceNotFoundException(code, getResource(code, args));
    }

    /**
     *
     * @param code
     * @param args
     */
    public void throwObjectNotFound(String code,Map<String,Object> addtion, Object... args) {
        throw new ResourceNotFoundException(code, getResource(code, args),addtion);
    }

    /**
     *
     * @param code
     * @param args
     */
    public void throwIllegalArgument(String code, Object... args) {
        throw new IllegalArgumentsException(code, getResource(code, args));
    }

    /**
     *
     * @param code
     * @param args
     */
    public void throwIllegalArgument(String code,Map<String,Object> addtion, Object... args) {
        throw new IllegalArgumentsException(code, getResource(code, args),addtion);
    }

    /**
     *
     * @param code
     * @param args
     */
    public void throwForbidden(String code, Object... args) {
        throw new ForbiddenException(code, getResource(code, args));
    }

    /**
     *
     * @param code
     * @param args
     */
    public void throwForbidden(String code,Map<String,Object> addtion, Object... args) {
        throw new ForbiddenException(code, getResource(code, args),addtion);
    }

    /**
     * 资源冲突
     * @param code
     * @param args
     */
    public void throwConflict(String code, Object... args) {
        throw new ConflictException(code, getResource(code, args));
    }

    /**
     * 资源冲突
     * @param code
     * @param args
     */
    public void throwConflict(String code, Map<String,Object> addtion, Object... args) {
        throw new ConflictException(code, getResource(code, args),addtion);
    }

    public void throwMethodNotSupport(){
        throw new MethodNotSupportException("-2",getResource("-2"));
    }
}
